import { Message, MessageBox } from 'element-ui'
import { remove, post, put } from '@/api/request'
const objType = {
  'warning': '警告：',
  'error': 'Error：',
  'success': ''
}
export function $tooltips(message, type = 'error', duration = 3 * 1000) {
  Message({
    message: objType[type] + message,
    type: type,
    duration: duration
  })
}
// export function $remove(api, _that, info = '确定删除吗?', method = 'DELETE') {
//   MessageBox.alert(info, '提示', {
//     confirmButtonText: '确定',
//     type: 'warning'
//   }).then((action) => {
//     if (action) {
//       if (method === 'DELETE') {
//         remove(api).then(() => {
//           _that.upData()
//         })
//       }
//       if (method === 'POST') {
//         post(api).then(() => {
//           _that.upData()
//         })
//       }
//       if (_that.show_details) _that.show_details = false
//     }
//   }).catch(() => {})
// }
export function $remove(api, _that, info = '确定删除吗?', method = 'DELETE') {
  return new Promise(function(resolve, reject) {
    MessageBox.confirm(info, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      showClose: false
    }).then((confirm) => {
      if (method === 'DELETE') {
        remove(api).then(() => {
          _that.upData()
        })
      }
      if (method === 'POST') {
        post(api).then(() => {
          _that.upData()
        })
      }
      if (_that.show_details) _that.show_details = false
    }).catch((cancel) => {
      reject(false)
    })
  })
}

export function $recover(api, _that, info = '确定恢复吗?', method = 'PUT') {
  return new Promise(function(resolve, reject) {
    MessageBox.confirm(info, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      showClose: false
    }).then((confirm) => {
      put(api).then(() => {
        _that.upData()
      })
    }).catch((cancel) => {
      reject(false)
    })
  })
}

export function $submit(content, api, param, _that) {
  MessageBox.alert(content, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then((action) => {
    if (action) {
      post(api, param).then((response) => {
        if (response.code === 0) {
          Message({
            message: '成功!',
            type: 'success'
          })
          _that.upData()
        }
      })
    }
  }).catch(() => {})
}

export function $dialogs(content, type = 'warning', confirmButtonText = '确定', cancelButtonText = '取消') {
  return new Promise(function(resolve, reject) {
    MessageBox.confirm(content, '提示', {
      confirmButtonText: confirmButtonText,
      cancelButtonText: cancelButtonText,
      type: type,
      showClose: false
    }).then((confirm) => {
      resolve(true)
    }).catch((cancel) => {
      reject(false)
    })
  })
}

export function $onFinishBox(content = '确定提交吗?', type = 'warning') {
  return new Promise(function(resolve, reject) {
    MessageBox.confirm(content, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: type,
      showClose: false
    }).then((confirm) => {
      resolve(true)
    }).catch((cancel) => {
      reject(false)
    })
  })
}

const onFinishBox = (content = '确定提交吗?', type = 'warning') => {
  return new Promise(function(resolve, reject) {
    MessageBox.confirm(content, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: type,
      showClose: false
    }).then((confirm) => {
      resolve(true)
    }).catch((cancel) => {
      reject(false)
    })
  })
}
export default onFinishBox

